﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UIWindows.Tools.Helper
{
    public static class ConvertHelper
    {
        /// <summary>
        /// 检测当前时间是否是年份交换或季节交换节点
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
       // public static bool ContrastTime(this DateTime dateTime) => (dateTime.Month == 1 || dateTime.Month == 4 || dateTime.Month == 7 || dateTime.Month == 10) && dateTime.Day == 1;
        public static bool ContrastTime(this DateTime dateTime) => (dateTime.Month == 1 || dateTime.Month == 4 || dateTime.Month == 7 || dateTime.Month == 10) && dateTime.Day <11;

        /// <summary>
        /// 计算季度===三个月为一季
        /// </summary>
        /// <param name="count">当前时间</param>
        /// <returns>1 = 1-3月；2 = 4-6月；3 = 7-9月；4 = 10-12月</returns>
        public static int GetQuarterly(this DateTime dateTime) => dateTime.Month switch
        {
            int p when p is >= 1 and < 4 => 1,
            int p when p is >= 4 and < 7 => 2,
            int p when p is >= 7 and < 10 => 3,
            int p when p is >= 10 and <= 12 => 4,
            _ => 1,
        };
        /// <summary>
        /// DateTime转13位时间戳
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static long GetJSTimeUtc(this DateTime dateTime)
        {            
            DateTime startTime = new DateTime(1970, 1, 1).ToLocalTime(); // 当地时区
            return (long)(dateTime - startTime).TotalMilliseconds; // 相差毫秒数
        }

        /// <summary>
        /// 时间戳转DateTime
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime GetDateTime(this long timeStamp) => DateTimeOffset.FromUnixTimeMilliseconds(timeStamp).LocalDateTime;


        /// <summary>
        /// TC60型号电子秤解析
        /// </summary>
        /// <param name="originalbyte">称重数据</param>
        /// <returns>值不正确为0</returns>
        public static double? GetTC60Weight(this byte[] originalbyte)
        {
            try
            {
                if (originalbyte.Length >= 20)
                {
                    byte[] bytes = new byte[0];
                    var str = Encoding.ASCII.GetString(originalbyte).Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    if (str.Length > 1)
                    {
                        int count = 0;
                        for (int i = str.Length - 1; i > 0; i--)
                        {
                            if (str[i].Length == 18
                                && !str[i].Contains("AL--1")
                                && str[i].Contains("ST,GS"))
                            {
                                count = i;
                                break;
                            }
                        }
                        bytes = Encoding.ASCII.GetBytes(str[count]);
                    }
                    else
                    {
                        bytes = Encoding.ASCII.GetBytes(str[0]);
                        if (bytes.Length < 18) return null;
                    }
                    if (bytes.Length != 18) return null;
                    var section1 = Encoding.ASCII.GetString(bytes, 0, 2);//ST
                    var section2 = Encoding.ASCII.GetString(bytes, 3, 2);//GS
                    var section3 = Encoding.ASCII.GetString(bytes, 6, 1);//+
                    var section4 = Encoding.ASCII.GetString(bytes, 7, 7);//value
                    double.TryParse(section4, out double w);
                    if (section1.ToUpper() == "ST" && !section3.Contains("-")) return w;
                }
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// AND AD 4531B型号电子秤解析
        /// </summary>
        /// <param name="originalbyte"></param>
        /// <returns></returns>
        public static double? GetAndWeight(this byte[] originalbyte)
        {
            try
            {
                if (originalbyte.Length >= 15)
                {
                    byte[] bytes = new byte[0];
                    var str = Encoding.ASCII.GetString(originalbyte).Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    if (str.Length > 1)
                    {
                        int count = 0;
                        for (int i = str.Length - 1; i > 0; i--)
                        {
                            if (str[i].Length == 13 && str[i].Contains("WT,+"))
                            {
                                count = i;
                                break;
                            }
                        }
                        bytes = Encoding.ASCII.GetBytes(str[count]);
                    }
                    else
                    {
                        bytes = Encoding.ASCII.GetBytes(str[0]);
                        if (bytes.Length < 13) return null;
                    }
                    var section1 = Encoding.ASCII.GetString(bytes, 0, 2);//WT
                    var section3 = Encoding.ASCII.GetString(bytes, 3, 1);//+
                    var section4 = Encoding.ASCII.GetString(bytes, 4, 7);//value
                    double.TryParse(section4, out double w);
                    if (section1.ToUpper() == "WT" && section3.Contains("+")) return w;
                }
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }


    }
}
